package cn.csc.module.product.controller.admin.spudetail;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.spudetail.vo.*;
import cn.csc.module.product.convert.spudetail.SpuDetailConvert;
import cn.csc.module.product.dal.dataobject.spudetail.SpuDetailDO;
import cn.csc.module.product.service.spudetail.SpuDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 商品详情信息")
@RestController
@RequestMapping("/product/spu-detail")
@Validated
public class SpuDetailController {

    @Resource
    private SpuDetailService spuDetailService;

    @PostMapping("/create")
    @ApiOperation("创建商品详情信息")
    @PreAuthorize("@ss.hasPermission('product:spu-detail:create')")
    public CommonResult<BigInteger> createSpuDetail(@Valid @RequestBody SpuDetailCreateReqVO createReqVO) {
        return success(spuDetailService.createSpuDetail(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商品详情信息")
    @PreAuthorize("@ss.hasPermission('product:spu-detail:update')")
    public CommonResult<Boolean> updateSpuDetail(@Valid @RequestBody SpuDetailUpdateReqVO updateReqVO) {
        spuDetailService.updateSpuDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品详情信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-detail:delete')")
    public CommonResult<Boolean> deleteSpuDetail(@RequestParam("id") BigInteger id) {
        spuDetailService.deleteSpuDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品详情信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-detail:query')")
    public CommonResult<SpuDetailRespVO> getSpuDetail(@RequestParam("id") BigInteger id) {
        SpuDetailDO spuDetail = spuDetailService.getSpuDetail(id);
        return success(SpuDetailConvert.INSTANCE.convert(spuDetail));
    }

    @GetMapping("/list")
    @ApiOperation("获得商品详情信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:spu-detail:query')")
    public CommonResult<List<SpuDetailRespVO>> getSpuDetailList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SpuDetailDO> list = spuDetailService.getSpuDetailList(ids);
        return success(SpuDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得商品详情信息分页")
    @PreAuthorize("@ss.hasPermission('product:spu-detail:query')")
    public CommonResult<PageResult<SpuDetailRespVO>> getSpuDetailPage(@Valid SpuDetailPageReqVO pageVO) {
        PageResult<SpuDetailDO> pageResult = spuDetailService.getSpuDetailPage(pageVO);
        return success(SpuDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出商品详情信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:spu-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportSpuDetailExcel(@Valid SpuDetailExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SpuDetailDO> list = spuDetailService.getSpuDetailList(exportReqVO);
        // 导出 Excel
        List<SpuDetailExcelVO> datas = SpuDetailConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品详情信息.xls", "数据", SpuDetailExcelVO.class, datas);
    }

}
